# -*- coding: utf-8 -*-

# FILE: LiteralFactory.py
# DESC: Factory implementation for Literal objects.

from EntityFactory import EntityFactory
from web.entity.Literal import Literal 
from infra.logging import logger


class LiteralFactory(EntityFactory):

	def __init__(self, dataFilters, lexer):
		EntityFactory.__init__(self)
		self.dataFilters_ = dataFilters
		self.data_ = None 
		self.lexer_ = lexer


	def validate(self, argv):
		log = logger.getLogger()
		rv = True
		data = argv
		for dataFilter in self.dataFilters_:
			data = dataFilter.filter(data)
		if not data:
			self.errMsg_ = "All data filtered out. LiteralFactory has nothing to work with."
			log.debug(self.errMsg_)
			rv = False
		else:
			self.data_ = data
		return rv 


	def doCreateEntity(self, consumer, relevanceFactor):
		log = logger.getLogger()
		log.debug("Creating Literal object from data = \"%s\" ..." % self.data_) 

		literal = Literal(self.data_, relevanceFactor)
		if consumer:
			consumer.onLiteral(literal, self.lexer_)

		log.debug("Literal object created.")

		return literal


	def getEntityType(self):
		# TODO: add unicode chars
		#return r"[a-zA-Z0-9'.!?,;:\- ]+"
		return r"[a-záéíĺóŕúýàèòùâêîôûšžčňěřťďľäöëüñãõåůçA-ZÁÉÍĹÓŔÚÝÀÈÒÙÂÊÎÔÛŠŽČŇĚŘŤĎĽÄÖÜÑÃÕÅŮÇ0-9`'.!?,;:\- ]+"
